// pages/main/erweima/erweima.js
import http from '../../../class/Utils/Http'
import user from '../../../class/Utils/User'
import tips from '../../../class/Utils/Tips'
Page({

    /**
     * 页面的初始数据
     */
    data: {
        btn_title: "下载二维码",
        btn_dis: false,
        opendownload: false,
        pyq_s: 0,
        gengxin_pic: 0,
        headimg: '',
        zhezhao: '',
        oktu: '',
    },
    onLoad: function () {
        var that = this
        var user = wx.getStorageSync('user')
        http.req("/Mingpian/user_mingpian_erweima", {uid: user.userid}, function (res) {
            console.log(res)
            that.setData({
                rinfo: res,
                uinfo: user,
                //pyq_pic: res.pyq_pic
            })
          
            // http.req("/Erweima/gengxin", {uid: user.userid, mid: res.mid}, function (pyqres) {
            //     if (pyqres.code == 200) {
            //         tips.loaded();
            //         that.setData({
            //             pyq_pic: pyqres.data
            //         })
            //       console.log(that.data.pyq_pic)
            //     } else {
            //         tips.error("更新失败!")
            //     }
            // })
            // if (res.pyq == 0) {
            //     tips.loading("更新中。。。")
            //     http.req("/Erweima/gengxin", {uid: user.userid, mid: res.mid}, function (pyqres) {
            //         if (pyqres.code == 200) {
            //             tips.loaded();
            //             that.setData({
            //                 pyq_pic: pyqres.data
            //             })
            //         } else {
            //             tips.error("更新失败!")
            //         }
            //     })
            // } else {
            //     pyq_pic:res.pyq_pic
            // }

          //获取二维码 begin
          wx.downloadFile({
            url: that.data.rinfo.erweima,
            success: function (sres) {
              console.log(sres);
              that.data.fxterweima = sres.tempFilePath;
              console.log(that.data.fxterweima);
            }, fail: function (fres) {
            }
          })
          //获取二维码 end
        })



      //读取用户信息
      var user = wx.getStorageSync('user');
      that.setData({
        user: user
      })
      var data = {
        userid: user.userid,
        qyid: that.data.qyid
      }
      http.req("/Mingpian/info", data, function (infores) {
        console.log(infores)
        that.setData({
          uderinfo: infores
        })
        console.log(that.data.uderinfo.pic)
        var userheadimg = that.data.uderinfo.pic
        if (userheadimg !== null) {
          console.log('开始生成朋友圈分享图')
        }else{
          tips.modal('缺少名片头像，去完善', false, 'guanbi', '/pages/mingpian/index/index')
        }

        //获取遮罩图 begin
        wx.downloadFile({
          url: 'https://toocle.y.netsun.com/xcximg/el-fxdpyqbg.png',
          success: function (sres) {
            that.data.zhezhao = sres.tempFilePath
            console.log(that.data.zhezhao);

            //获取头像 begin
            // var huoqutouxiang = that.data.uderinfo.pic

            if (userheadimg !== null){
              wx.downloadFile({
                url: that.data.uderinfo.pic,
                success: function (sres) {
                  console.log(sres);
                  that.data.headimg = sres.tempFilePath;
                  console.log(that.data.headimg);
                  that.huizhisharepic()
                }, fail: function (fres) {

                }
              })
            }else{
              //  tips.modal('缺少名片信息', false, 'guanbi', '/pages/mingpian/index/index')
            }
            

        //获取头像 end
          }, fail: function (fres) {
          }
        })
         //获取遮罩图 end
        
        
        



      })


      



      
    },

  huizhisharepic: function () {
    var that = this;
    //1. 请求后端API生成小程序码
    //that.getQr();

    //2. canvas绘制文字和图片
    const ctx = wx.createCanvasContext('myCanvas');
    var imgPatha = that.data.headimg;
    var bgImgPath = that.data.zhezhao;
    var fxtgongsi = that.data.uderinfo.gongsi
    var fxtxingming = that.data.uderinfo.xingming
    var fxtzhiwei = that.data.uderinfo.zhiwei
    var fxtshouji = that.data.uderinfo.shouji
    var fxtyouxiang = that.data.uderinfo.youxiang
    var fxtdizhi = that.data.uderinfo.dizhi
    var fxterweima = that.data.fxterweima
    var xcxcode = that.data.fxterweima;//二维码

    





    //填充背景
    ctx.setFillStyle('#f8f8f8');
    ctx.fillRect(0, 0, 375, 667);



    //绘制头像
    ctx.drawImage(imgPatha, 15, 37, 202, 202);

    //绘制遮罩
    ctx.drawImage(bgImgPath, 0, 0, 375, 604);

    //绘制公司名
    ctx.setFontSize(12);
    ctx.setTextAlign('right')
    ctx.setFillStyle('#000000');
    
    //转换单行 begin
    var chrgongsi = fxtgongsi.split("");//这个方法是将一个字符串分割成字符串数组
    var tempgongsi = "";
    for (var a = 0; a < chrgongsi.length; a++) {
      tempgongsi += chrgongsi[a];
      //console.log("sssssssss", tempgongsi)
      if (ctx.measureText(tempgongsi).width > 110) {
        tempgongsi += '...';
        break;
      }
    }
    ctx.fillText(tempgongsi, 351, 62);
    //转换单行 end


    //绘制姓名
    ctx.setFontSize(16);
    ctx.setTextAlign('left')
    ctx.setFillStyle('#ffffff');
    ctx.fillText(fxtxingming, 220, 108);

    //绘制职位
    ctx.setFontSize(12);
    ctx.setFillStyle('#c5ceeb');
    ctx.fillText(fxtzhiwei, 220, 126);


    //绘制名片内容
    ctx.setFontSize(12);
    ctx.setTextAlign('right')
    ctx.setFillStyle('#333333');
    ctx.fillText(fxtshouji, 332, 163);
    ctx.fillText(fxtyouxiang, 332, 181);

    //拆分换行地址 begin
    var chrdizhi = fxtdizhi.split("");//这个方法是将一个字符串分割成字符串数组
    var tempdizhi = "";
    var rowdizhi = [];
    for (var a = 0; a < chrdizhi.length; a++) {
      if (ctx.measureText(tempdizhi).width < 150) {
        tempdizhi += chrdizhi[a];
      }
      else {
        a--; //这里添加了a-- 是为了防止字符丢失，效果图中有对比
        rowdizhi.push(tempdizhi);
        tempdizhi = "";
      }
    }
    rowdizhi.push(tempdizhi);
    if (rowdizhi.length > 2) {
      var rowCut = rowdizhi.slice(0, 2);
      var rowPart = rowCut[1];
      var test = "";
      var empty = [];
      for (var a = 0; a < rowPart.length; a++) {
        if (ctx.measureText(test).width < 140) {
          test += rowPart[a];
        }
        else {
          break;
        }
      }
      empty.push(test);
      var group = empty[0] + "..."//这里只显示两行，超出的用...表示
      rowCut.splice(1, 1, group);
      rowdizhi = rowCut;
    }
    for (var b = 0; b < rowdizhi.length; b++) {
      ctx.fillText(rowdizhi[b], 332, 200 + b * 15, 200);
    }

    //拆分换行地址 begin
    


    //绘制文字
    ctx.setFontSize(18);
    ctx.setTextAlign('center')
    ctx.setFillStyle('#333333');
    ctx.fillText('长按识别二维码 收下名片', 187.5, 580);

    //绘制二维码
    ctx.drawImage(xcxcode, 69, 280, 238, 238);


    ctx.draw(true, function () {
      that.huoqusharetu()
    });
  },


  //保存图片代码
  savetup: function () {
    var that = this;
      tips.loading("保存中...")
    wx.canvasToTempFilePath({
      x: 0,
      y: 0,
      width: 375,
      height: 604,
      destWidth: 800,
      destHeight: 1289,
      quality: 1,
      canvasId: 'myCanvas',
      success: function (res) {
        //调取小程序当中获取图片
        console.log(res.tempFilePath);
        // that.setData({
        //   oktu: res.tempFilePath
        // })
        //console.log(that.data.oktu);
        wx.saveImageToPhotosAlbum({
          filePath: res.tempFilePath,
          success:function(res) {
              tips.loaded()
           tips.ok("下载成功!")
          }
        })
      },
      fail: function (res) {
        console.log(res)
      }
    })
  },
  //获取生成后的分享图
  huoqusharetu: function () {
    var that = this;
    wx.canvasToTempFilePath({
      x: 0,
      y: 0,
      width: 375,
      height: 604,
      destWidth: 800,
      destHeight: 1289,
      quality: 1,
      canvasId: 'myCanvas',
      success: function (res) {
        //调取小程序当中获取图片
        console.log(res.tempFilePath);
        that.setData({
          oktu: res.tempFilePath
        })
        console.log(that.data.oktu);

      },
      fail: function (res) {
        console.log(res)
      }
    })
  },






























    getinfo: function () {

        var that = this
        var user = wx.getStorageSync('user')
        http.req("/Mingpian/user_mingpian_erweima", {uid: user.userid}, function (res) {
            console.log(res)
            that.setData({
                rinfo: res,
                uinfo: user,
                pyq_pic: res.pyq_pic
            })
        })
    },
    // gengxin: function () {
    //     var that = this
    //     var user = wx.getStorageSync('user')
    //     var data = {
    //         uid: user.userid,
    //         mid: that.data.rinfo.mid
    //     }
    //     http.req("/Erweima/gengxin", data, function (pyqres) {
    //         console.log(pyqres)
    //         if (pyqres.code == 200) {
    //             that.getinfo()
    //         } else {
    //             tips.error("更新失败!")
    //         }
    //     })
    //
    // },
    downerweima: function () {
        var that = this
        var user = wx.getStorageSync('user');
        that.setData({
            btn_title: "保存中。。",
            btn_dis: true
        })
        tips.loading("保存中")
        wx.downloadFile({
            url: that.data.rinfo.erweima,
            success: function (suc) {
                console.log(suc)
                if (suc.statusCode === 200) {
                    wx.saveImageToPhotosAlbum({
                        filePath: suc.tempFilePath,
                        success: function (res) {
                            tips.loaded()
                            tips.ok("下载成功!")
                            that.setData({
                                btn_title: "下载二维码",
                                btn_dis: false,
                                opendownload: !that.data.opendownload
                            })
                        },
                        fail: function (res) {
                            tips.loaded()
                            tips.error("下载失败")
                            that.setData({
                                btn_title: "下载二维码",
                                btn_dis: false,
                                opendownload: !that.data.opendownload
                            })
                        }
                    })
                }

            }

        })
    },
    downerweima_pyq: function () {
        var that = this
        var user = wx.getStorageSync('user');
        that.setData({
            btn_title: "保存中。。",
            btn_dis: true
        })
        tips.loading("保存中")
        wx.downloadFile({
            url: that.data.rinfo.pyq_pic,
            success: function (suc) {
                console.log(suc)
                if (suc.statusCode === 200) {
                    wx.saveImageToPhotosAlbum({
                        filePath: suc.tempFilePath,
                        success: function (res) {
                            tips.loaded()
                            tips.ok("下载成功!")
                            that.setData({
                                btn_title: "下载二维码",
                                btn_dis: false,
                                opendownload: !that.data.opendownload
                            })
                        },
                        fail: function (res) {
                            tips.loaded()
                            tips.error("下载失败")
                            that.setData({
                                btn_title: "下载二维码",
                                btn_dis: false,
                                opendownload: !that.data.opendownload
                            })
                        }
                    })
                }

            }

        })
    },
    //打开下载菜单
    download: function () {
        var that = this
        that.setData({
            opendownload: !that.data.opendownload
        })
    },
  fuzhuerweimadizhi: function (e) {
    var that = this;
    wx.setClipboardData({
      data: that.data.rinfo.erweima,
      success: function (res) {
          wx.showModal({
            title: '提示',
            content: '复制成功',
            success: function (res) {
              if (res.confirm) {
                console.log('确定')
              } else if (res.cancel) {
                console.log('取消')
              }
            }
          })
      },
      fail: function (res) {
        // wx.showModal({
        //   title: '提示',
        //   content: '没有微信号',
        //   success: function (res) {
        //     if (res.confirm) {
        //       console.log('确定')
        //     } else if (res.cancel) {
        //       console.log('取消')
        //     }
        //   }
        // })
      }
    });
  },

})